package com.pinyoumei.app.activity.wallet;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.activity.MainUIActivity;
import com.pinyoumei.app.activity.integral.IntegralShopActivity;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.Utils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

public class WalletHostActivity extends BaseActivityCurrency {

    private TextView whvItemMoney,whvItemNum,jfItemNum;

    @Override
    protected int layoutId() {
        return R.layout.wallet_host_view;
    }

    @Override
    protected void initView() {
        EventBusUtils.register(this);
        TextView textView = findViewById(R.id.include_title);
        textView.setText("我的钱包");
        findViewById(R.id.include_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.whv_item_layout_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("intentType",1);
                bundle.putString("intentNum",whvItemMoney.getText().toString());
                startActivityForBundle(WalletTransactionActivity.class,bundle);
            }
        });
        findViewById(R.id.whv_item_layout_two).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("intentType",2);
                bundle.putString("intentNum",whvItemNum.getText().toString());
                startActivityForBundle(WalletTransactionActivity.class,bundle);
            }
        });
        findViewById(R.id.whv_item_layout_three).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("intentType",3);
                bundle.putString("intentNum",whvItemNum.getText().toString());
                startActivityForBundle(WalletTransactionActivity.class,bundle);
            }
        });

        whvItemMoney = findViewById(R.id.whv_item_money);
        whvItemNum = findViewById(R.id.whv_item_num);
        jfItemNum = findViewById(R.id.jf_item_num);
    }

    @Override
    protected void initData() {
        findViewById(R.id.wha_one_btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForIntent(WalletHappyGroupActivity.class);
            }
        });
        findViewById(R.id.wha_two_btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainUIActivity.getInstance.Select_Position(1);
                EventBusUtils.post("finsh");
                finish();
            }
        });
        findViewById(R.id.wha_three_btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForIntent(IntegralShopActivity.class);
            }
        });
        findViewById(R.id.wha_one_btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForIntent(RechargeActivity.class);
            }
        });
        findViewById(R.id.wha_two_btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                buyHappyCouponInit(SPUtils.getAppUserId(WalletHostActivity.this));
                startActivityForIntent(HappyCouponToScoreInitActivity.class);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        myWalletRequest(SPUtils.getAppUserId(this));
    }

    private void myWalletRequest(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.CENTER_MYWALLET)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        CenterWalletBean centerWalletBean = gson.fromJson(response.body(), CenterWalletBean.class);
                        whvItemMoney.setText(centerWalletBean.getResult().getData_info().getUser_balance());
                        whvItemNum.setText(centerWalletBean.getResult().getData_info().getHappy_coupon());
                        jfItemNum.setText(centerWalletBean.getResult().getData_info().getUser_score());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(String message) {
        if(TextUtils.equals(message,"finsh")){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(this);
    }

    private void buyHappyCouponInit(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.buyHappyCouponInit)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        HappyCouponInitBean happyCouponInitBean = new Gson().fromJson(response.body(), HappyCouponInitBean.class);
                        Utils.red_package_amount = Double.parseDouble(happyCouponInitBean.getResult().getRed_package_amount());
                        Utils.happy_coupon = happyCouponInitBean.getResult().getHappy_coupon();
                        if(happyCouponInitBean.getCode() == 2){
                            startActivityForIntent(WalletRechargeActivity.class);
                        }else {
                            new WalletCouponDialog(WalletHostActivity.this,0,new int[]{}).show();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }
}
